package com.example.common.b_pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Data
public class BAdminUser extends BAdmin implements UserDetails {


    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> allAuthorities = new ArrayList<>();

        for (BRole role : roles) {
            // 处理角色权限
            String[] roleNames = role.getName().split(",");
            for (String name : roleNames) {
                allAuthorities.add(new SimpleGrantedAuthority("ROLE_" + name));
            }

            // 处理菜单权限
            for (BMenu menu : role.getMenuslist()) {
                String permission = menu.getMenuName();
                allAuthorities.add(new SimpleGrantedAuthority("PERMISSION_" + permission));
            }
        }

        return allAuthorities;
    }

    @Override
    public String getUsername() {
        return super.getAdmin();
    }

    @Override
    public String getPassword() {
        return super.getPassword();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
